﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Collections;
using System.Security.Cryptography;

namespace Radiation___IM
{
    public class Client
    {
        private Int64 m_key;
        private bool m_keyset;
        private Socket m_handler;
        private StringBuilder m_message;
        private Hashtable m_channels;
        private string m_xml_public_key;
        private RSACryptoServiceProvider m_prodiver;
        private AESManager m_aes;
        private bool m_invisible;

        private string m_nickname;

        public Int64 Key
        {
            get
            {
                return m_key;
            }
        }

        public RSACryptoServiceProvider RSAProvider
        {
            get
            {
                return m_prodiver;
            }
            set
            {
                m_prodiver = value;
            }
        }

        public AESManager AESProvider
        {
            get
            {
                return m_aes;
            }
            set
            {
                m_aes = value;
            }
        }

        public Socket Handler
        {
            get
            {
                return m_handler;
            }
        }

        public StringBuilder Message
        {
            get
            {
                return m_message;
            }
        }

        public Hashtable Channels
        {
            get
            {
                return m_channels;
            }
        }

        public string Nickname
        {
            get
            {
                return m_nickname;
            }
            set
            {
                m_nickname = value;
            }
        }

        public bool Invisible
        {
            get
            {
                return m_invisible;
            }
            set
            {
                m_invisible = value;
            }
        }

        public string PublicKeyXML
        {
            get
            {
                return m_xml_public_key;
            }
            set
            {
                m_xml_public_key = value;
            }
        }

        public byte[] Buffer;
        public readonly int BufferSize = 65565;

        public Client(Socket handler)
        {
            m_key = -1;
            m_keyset = false;
            m_handler = handler;
            Buffer = new byte[BufferSize];
            m_message = new StringBuilder("");
            m_nickname = "";
            m_xml_public_key = "";
            m_channels = new Hashtable();
            m_aes = new AESManager();
            m_invisible = false;
        }

        public rvalue setKey(Int64 key)
        {
            if (!m_keyset)
            {
                m_key = key;
                m_keyset = true;
                return rvalue.CLIENT_KEY_SET;
            }
            else
                return rvalue.CLIENT_KEY_ALREADY_SET;
        }

        public rvalue add(Channel chan)
        {
            if (!m_channels.ContainsKey(chan.Name))
            {
                m_channels.Add(chan.Name, chan);
                return rvalue.CHANNEL_ADDED;
            }
            else
                return rvalue.CHANNEL_ALREADY_EXISTS;
        }

        public rvalue delete(Channel chan)
        {
            if (m_channels.ContainsKey(chan.Name))
            {
                m_channels.Remove(chan.Name);
                return rvalue.CHANNEL_DELETED;
            }
            else
                return rvalue.CHANNEL_NOT_FOUND;
        }

    }
}
